package com.example.sunmiv1.adapter;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.CreditVipListInfo;
import com.example.sunmiv1.views.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddCreditUserAdapter extends BaseAdapter {

    private List<CreditVipListInfo.DataBean.ListBean> list;
    private ViewHolder vh;
    private CallBack callBack;

    public AddCreditUserAdapter(List<CreditVipListInfo.DataBean.ListBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_add_credit_user, null);
        vh = new ViewHolder(convertView);
        CreditVipListInfo.DataBean.ListBean bean = list.get(position);
        if (bean.isCheck()) {
            vh.itemAddCreditUserCheckImg.setImageResource(R.mipmap.repay_x);
        } else {
            vh.itemAddCreditUserCheckImg.setImageResource(R.mipmap.repay_wx);
        }
        if (TextUtils.isEmpty(bean.getAvatar()) || "-".equals(bean.getAvatar())) {
            vh.itemCreditUserImg.setImageResource(R.mipmap.default_img);
        } else {
            RequestOptions options = new RequestOptions();
            options.override(300, 300);
            options.placeholder(R.mipmap.default_img);//佔位圖  .error(R.mipmap.error_food)//如果是錯誤的情況
            Glide.with(parent.getContext())
                    .load(list.get(position).getAvatar())
                    .apply(options)
                    .into(vh.itemCreditUserImg);
        }
        vh.itemAddCreditUserName.setText(bean.getName());
        vh.itemAddCreditUserPhone.setText(bean.getMobile_phone());
        vh.itemAddCreditUserNick.setText(bean.getNickname());
//        vh.itemAddCreditUserMoneyTv.setText(bean.getQuota());
        vh.itemAddCreditUserMoneyEdit.setText(bean.getQuota());
        vh.itemAddCreditUserMoneyEdit.setSelection(bean.getQuota().length());
        final ImageView img = vh.itemAddCreditUserCheckImg;
        final int index = position;
        vh.itemAddCreditUserCheckLin.setOnClickListener(v -> {
            if (bean.isCheck()) {
                img.setImageResource(R.mipmap.repay_wx);
            } else {
                img.setImageResource(R.mipmap.repay_x);
            }
            bean.setCheck(!bean.isCheck());
            callBack.click(bean.isCheck() ? 1 : 2, index);
        });

        final TextView editMoney = vh.itemAddCreditUserEditMoney;
        editMoney.setText("(請設置賒賬額度,額度區間$" + 0 +
                "~$" + bean.getQuota()+")");
//        final TextView moneyTv = vh.itemAddCreditUserMoneyTv;
        final EditText editText = vh.itemAddCreditUserMoneyEdit;
//        editMoney.setOnClickListener(v -> {
//            if (editText.getVisibility() == View.VISIBLE) {
//                moneyTv.setVisibility(View.VISIBLE);
//                editText.setVisibility(View.GONE);
//                moneyTv.setText(bean.getQuota());
//            } else {
//                moneyTv.setVisibility(View.GONE);
//                editText.setVisibility(View.VISIBLE);
//                editText.postDelayed(() -> editText.requestFocus(), 500);
//            }
//        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                String temp = editText.getText().toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0) return;
                if (temp.length() - posDot - 1 > 2) {
                    editText.setText(temp.substring(0,posDot+3));
                    editText.setSelection(posDot+3);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                list.get(index).setQuota(editText.getText().toString());
            }
        });
        return convertView;
    }

    public interface CallBack {
        void click(int type, int index);//type:1增加 2:減少
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    static class ViewHolder {
        @BindView(R.id.itemAddCreditUser_checkImg)
        ImageView itemAddCreditUserCheckImg;
        @BindView(R.id.itemAddCreditUser_checkLin)
        LinearLayout itemAddCreditUserCheckLin;
        @BindView(R.id.itemCreditUser_img)
        CircleImageView itemCreditUserImg;
        @BindView(R.id.itemAddCreditUser_name)
        TextView itemAddCreditUserName;
        @BindView(R.id.itemAddCreditUser_phone)
        TextView itemAddCreditUserPhone;
        @BindView(R.id.itemAddCreditUser_nick)
        TextView itemAddCreditUserNick;
//        @BindView(R.id.itemAddCreditUser_moneyTv)
//        TextView itemAddCreditUserMoneyTv;
        @BindView(R.id.itemAddCreditUser_moneyEdit)
        EditText itemAddCreditUserMoneyEdit;
        @BindView(R.id.itemAddCreditUser_editMoney)
        TextView itemAddCreditUserEditMoney;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
